package com.medical.data.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.medical.data.domain.Department;
import com.medical.data.domain.HospitalInformation;
import com.medical.data.domain.User;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.DepartmentMapper;
import com.medical.data.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.service.HospitalInformationService;
import com.medical.data.service.UserService;
import com.medical.data.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private HospitalInformationService hospitalInformationService;

    @Autowired
    private UserService userService;

    @Override
    public List<Department> getKsByHsId(String id) {
        List<Department>list=new ArrayList<>();
        LambdaQueryWrapper<Department> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Department::getHospitalId, id)
                .eq(Department::getDepartmentName,"内科");
        list.add(this.baseMapper.selectOne(wrapper1));

        LambdaQueryWrapper<Department> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Department::getHospitalId, id)
                .eq(Department::getDepartmentName,"儿科");
        list.add(this.baseMapper.selectOne(wrapper2));

        LambdaQueryWrapper<Department> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(Department::getHospitalId, id)
                .eq(Department::getDepartmentName,"骨科");
        list.add(this.baseMapper.selectOne(wrapper3));
        return list;
    }

    @Override
    public void addDepartment(Department department) {
        //首先查询医院是否存在
        HospitalInformation hospitalInformation = hospitalInformationService.getById(department.getHospitalId());
        if (ObjectUtils.isEmpty(hospitalInformation))
            throw new MyException(ResultCode.ERROR.getCode(), "医院不存在");
        //查询是否有相同的科室
        Department department1 = baseMapper.selectOne(Wrappers.<Department>lambdaQuery().eq(Department::getDepartmentName, department.getHospitalId()).eq(Department::getHospitalId, department.getHospitalId()).select(Department::getId));
        if (!ObjectUtils.isEmpty(department1))
            throw new MyException(ResultCode.ERROR.getCode(), "科室已存在");
        int insert = baseMapper.insert(department);
        if (insert < 1)
            throw new MyException(ResultCode.ERROR.getCode(), "新增科室失败");
    }

    @Override
    public void updateDepartment(Department department) {
        HospitalInformation hospitalInformation = hospitalInformationService.getById(department.getHospitalId());
        if (ObjectUtils.isEmpty(hospitalInformation))
            throw new MyException(ResultCode.ERROR.getCode(), "医院不存在");
        //查询科室是否修改了名字
        Department department1 = baseMapper.selectById(department.getId());
        if (StrUtil.equals(department1.getDepartmentName(), department.getDepartmentName())) {
            int i = baseMapper.updateById(department);
            if (i < 1)
                throw new MyException(ResultCode.ERROR.getCode(), "修改失败");
        }
        //名字不一致，查询是否有相同的名字
        Department department2 = baseMapper.selectOne(Wrappers.<Department>lambdaQuery().eq(Department::getDepartmentName, department.getHospitalId()).eq(Department::getHospitalId, department.getHospitalId()).select(Department::getId));
        if (!ObjectUtils.isEmpty(department2)) {
            throw new MyException(ResultCode.ERROR.getCode(), "该科室已存在");
        }
        int i = baseMapper.updateById(department);
        if (i < 1)
            throw new MyException(ResultCode.ERROR.getCode(), "修改失败");
    }

    @Override
    public List<User> getUserByDepartmentId(String departmentId) {
        List<User> users = userService.getUserByDepartmentId(departmentId);
        return users;
    }

    @Override
    public List<Department> getDepartments(String hospitalId) {
        //检验hospitalId是否正确
        hospitalInformationService.isExistHospital(hospitalId);

        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getHospitalId, hospitalId);
        List<Department> departments = new ArrayList<>();
        departments = this.baseMapper.selectList(wrapper);
        return departments;
    }

    @Override
    public String getDepartmentById(String departmentId) {
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getId, departmentId);
        return this.baseMapper.selectOne(wrapper).getDepartmentName();
    }
}
